<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px"
    >
      <el-form-item label="时段名字" prop="timeIntervalName">
        <el-input v-model="dataForm.timeIntervalName" placeholder="时段名字"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="dataForm.status" label="1">营业</el-radio>
        <el-radio v-model="dataForm.status" label="2">休息</el-radio>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-time-select
          placeholder="起始时间"
          v-model="dataForm.startTime"
          :picker-options="{
         start: '00:00',
         step: '00:15',
         end: '24:00'
       }"
        >
        </el-time-select>
        <el-time-select
          placeholder="结束时间"
          v-model="dataForm.endTime"
          :picker-options="{
         start: '00:00',
         step: '00:15',
         end: '24:00',
         minTime: dataForm.startTime
       }"
        >
        </el-time-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      disabledSubmit: false,
      dataForm: {
        timeIntervalId: 0,
        timeIntervalName: '',
        sellerId: '',
        status: '1',
        startTime: '',
        endTime: '',
        remark: ''
      },
      dataRule: {
        timeIntervalName: [
          { required: true, message: '时段名字不能为空', trigger: 'blur' }
        ],
        sellerId: [
          { required: true, message: '所属租户不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态(1营业2休息)不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.timeIntervalId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.timeIntervalId) {
          this.$http({
            url: this.$http.adornUrl(`/store/tstoretimeinterval/info/${this.dataForm.timeIntervalId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.timeIntervalName = data.tStoreTimeInterval.timeIntervalName
              this.dataForm.sellerId = data.tStoreTimeInterval.sellerId
              this.dataForm.status = data.tStoreTimeInterval.status.toString()
              this.dataForm.startTime = data.tStoreTimeInterval.startTime
              this.dataForm.endTime = data.tStoreTimeInterval.endTime
              this.dataForm.remark = data.tStoreTimeInterval.remark
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.disabledSubmit = true
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/store/tstoretimeinterval/${!this.dataForm.timeIntervalId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'timeIntervalId': this.dataForm.timeIntervalId || undefined,
              'timeIntervalName': this.dataForm.timeIntervalName,
              'sellerId': this.dataForm.sellerId,
              'status': this.dataForm.status,
              'startTime': this.dataForm.startTime,
              'endTime': this.dataForm.endTime,
              'remark': this.dataForm.remark
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.disabledSubmit = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          this.disabledSubmit = false
        }
      })
    }
  }
}
</script>
